﻿using DLLPC.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace VisionCore
{
    public class Status_Sort : ProcedureHelper_Exc
    {
        string subStatusCode = "";
        string tempStatusCode = "";
        bool condition = false;
        bool start = false;

        int count;
        SystemTimeCounter scStatus = new SystemTimeCounter();
        public void Main()
        {
            condition = /*SortST_Presence &&*/ /*!ParameterHelper.SortST.SortCompletFlag &&*/ !ExcTR_FlipReq
                     && Kernel.motion.ExcTR_X.IsHomeAlready && Kernel.motion.ExcTR_R.IsHomeAlready && Kernel.motion.ExcTR_Z.IsHomeAlready
                     && Kernel.motion.SortST_X.IsHomeAlready && Kernel.motion.SortST_Y.IsHomeAlready && Kernel.motion.SortST_R.IsHomeAlready;
            start = condition && SortSTStatusCode == SortSTStatus.Sorter;

            switch (SortStatusCode)
            {
                case SortStatus.Init:
                    subStatusCode = "00: 准备";
                    {
                        SortPos_Sort_X = ParameterHelper.SysParam.SortST.Wait.X;
                        SortPos_Sort_Y = ParameterHelper.SysParam.SortST.Wait.Y;
                        if (start)
                        {
                            if (!DryRunMode)
                            {
                                string ID = ParameterHelper.SortST.Wafer_ID;
                                string path = $"M:\\{ID}";
                                string pathdest = Environment.CurrentDirectory + string.Format(@"\WaferResult_ToMotion\{0}", ID.ToString());

                                    if (File.Exists(path))
                                    {
                                        File.Delete(pathdest);
                                        File.Copy(path, pathdest);
                                        //ParameterHelper.SortST.ReadMapFlag = true;
                                        SaveSortST();
                                    }
                                    CurrentDieIndex = 0;
                                
                                if (File.Exists(pathdest))
                                {
                                    WaferResult_ToMotion = Utilities.SerializeHelper.Deserialize<WaferResult_ToMotion>(pathdest);
                                }
                                else
                                {
                                    return;
                                }
                                SortStatusCode = SortStatus.CheckStatus;
                            }
                            else
                            {
                                SortStatusCode = SortStatus.AlignReq;
                            }
                        }
                    }
                    break;
                case SortStatus.CheckStatus:
                    subStatusCode = "05: 状态判断";
                    {
                        if (WaferResult_ToMotion != null &&!ExcTR_FlipReq /*&& !ParameterHelper.SortST.SortCompletFlag*/)
                        {
                            if (WaferResult_ToMotion.Dies.Count > 0 && CurrentDieIndex < WaferResult_ToMotion.Dies.Count)
                            {
                                if (!WaferResult_ToMotion.Dies[CurrentDieIndex].Taked)
                                {
                                    if (!WaferResult_ToMotion.Dies[CurrentDieIndex].CCD_JingPass)
                                    {
                                        SortSTFinalAlignPass = false;
                                        if (WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.x <= -140
                                                && WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.x >= -360
                                                && WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.y <= -200
                                                && WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.y >= -470 
                                                && WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.StageR <= 91
                                                && WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.StageR >= -95)
                                        {
                                            SortPos_Sort_X = WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.x;
                                            SortPos_Sort_Y = WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.y;
                                            SortPos_Sort_R = WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.StageR;
                                            SortStatusCode = SortStatus.ToDieUnloadBeforeAlign;
                                        }
                                        else
                                        {
                                            if (!(WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.x <= -140 && WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.x >= -360))
                                            {
                                                ProcedureHelper_Exc.LogMsg(string.Format("挑拣精定位X位置超范围：结果[OK:-360<= X <= -140]:{0}", WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.x));
                                            }
                                            if (!(WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.y <= -200 && WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.y >= -470))
                                            {
                                                ProcedureHelper_Exc.LogMsg(string.Format("挑拣精定位Y位置超范围：结果[OK:-200<= Y <= -470]:{0}", WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.y));
                                            }
                                            if (!(WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.StageR <= 90&& WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.StageR >= -90))
                                            {
                                                ProcedureHelper_Exc.LogMsg(string.Format("挑拣精定位StageR位置超范围：结果[OK:-95<= Y <= 91]:{0}", WaferResult_ToMotion.Dies[CurrentDieIndex].Locate.StageR));
                                            }
                                            WaferResult_ToMotion.Dies[CurrentDieIndex].Taked = true;
                                            CurrentDieIndex++;
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        SortSTFinalAlignPass = true;
                                        if ((LDTRStatusCode == LDTRStatus.ToLoad || LDTRStatusCode == LDTRStatus.WaitTR_1_LDAndTR_2_ULD )&& !LDTR_1_Pass /*&& LDTR_1_Empty*/)
                                        {
                                            if (WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.x <= -140 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.x >= -360 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.y <= -200 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.y >= -470 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.r <=92 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.r >= -180 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.StageR <= 91 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.StageR >= -95)
                                            {
                                                SortPos_Sort_X = WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.x;
                                                SortPos_Sort_Y = WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.y;
                                                SortPos_Sort_R = WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.StageR;
                                                SortPos_LDTR_1_R = WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.r;

                                                SortStatusCode = SortStatus.ToDieUnloadAfterAlign;
                                            }
                                            else
                                            {
                                                if (!(WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.x <= -140 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.x >= -360))
                                                {
                                                    ProcedureHelper_Exc.LogMsg(string.Format("初对位-挑拣LDTR1,X位置超范围：结果[OK:-360<= X <= -140]:{0}", WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.x));
                                                }
                                                if (!(WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.y <= -200 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.y >= -470))
                                                {
                                                    ProcedureHelper_Exc.LogMsg(string.Format("初对位-挑拣LDTR1,Y位置超范围：结果[OK:-470<= Y <= -200]:{0}", WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.y));
                                                }
                                                if (!(WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.StageR <= 90 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.StageR >= -90))
                                                {
                                                    ProcedureHelper_Exc.LogMsg(string.Format("初对位-挑拣LDTR1,Stage R位置超范围：结果[OK:-95<= R <=91]:{0}", WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.StageR));
                                                }
                                                if (!(WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.r <=92 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.r >=-180))
                                                {
                                                    ProcedureHelper_Exc.LogMsg(string.Format("初对位-挑拣LDTR1,R位置超范围：结果[OK:-180<= R <=92]:{0}", WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw1.r));
                                                }
                                                WaferResult_ToMotion.Dies[CurrentDieIndex].Taked = true;
                                                CurrentDieIndex++;
                                                return;
                                            }
                                        }
                                        else if ((LDTRStatusCode == LDTRStatus.ToUnload || LDTRStatusCode == LDTRStatus.WaitTR_1_ULDAndTR_2_LD) && !LDTR_2_Pass /*&& LDTR_2_Empty*/)
                                        {
                                            if (WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.x <= -140 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.x >= -360 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.y <= -200 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.y >= -470 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.r <= 92 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.r >= -180 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.StageR <= 91 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.StageR >= -95)
                                            {
                                                SortPos_Sort_X = WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.x;
                                                SortPos_Sort_Y = WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.y;
                                                SortPos_Sort_R = WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.StageR;
                                                SortPos_LDTR_2_R = WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.r;

                                                SortStatusCode = SortStatus.ToDieUnloadAfterAlign;
                                            }
                                            else
                                            {
                                                if (!(WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.x <= -140 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.x >= -360))
                                                {
                                                    ProcedureHelper_Exc.LogMsg(string.Format("初对位-挑拣LDTR2,X位置超范围：结果[OK:-360<= X <= -140]:{0}", WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.x));
                                                }
                                                if (!(WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.y <= -200 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.y >= -470))
                                                {
                                                    ProcedureHelper_Exc.LogMsg(string.Format("初对位-挑拣LDTR2,Y位置超范围：结果[OK:-470<= Y <= -200]:{0}", WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.y));
                                                }
                                                if (!(WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.StageR <= 90 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.StageR >= -90))
                                                {
                                                    ProcedureHelper_Exc.LogMsg(string.Format("初对位-挑拣LDTR2,Stage R位置超范围：结果[OK:-95<= R <=91]:{0}", WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.StageR));
                                                }
                                                if (!(WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.r <=92 && WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.r >=-180))
                                                {
                                                    ProcedureHelper_Exc.LogMsg(string.Format("初对位-挑拣LDTR2,R位置超范围：结果[OK:-180<= R <=92]:{0}", WaferResult_ToMotion.Dies[CurrentDieIndex].Jaw2.r));
                                                }
                                                WaferResult_ToMotion.Dies[CurrentDieIndex].Taked = true;
                                                CurrentDieIndex++;
                                                return;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    CurrentDieIndex++;
                                    return;
                                }
                            }
                            else
                            {
                                //ParameterHelper.SortST.SortCompletFlag = true;
                                SaveSortST();
                                CurrentDieIndex = 0;
                                SortStatusCode = SortStatus.Init;
                            }
                        }
                        else
                        {
                            SortStatusCode = SortStatus.Init;
                        }
                    }
                    break;
                case SortStatus.ToDieUnloadBeforeAlign:
                    subStatusCode = "06: SortST_XYR移动到DieULD对位位";
                    {
                        SortSTFinalAlignOK = false;
                        SortSTFinalAlignNGPass = false;
                        if (SortST_X_Sort && SortST_Y_Sort && SortST_R_Sort && LDTR_R_ALG && LDTR_CCD_ALG )
                        {
                            SortStatusCode = SortStatus.AlignReq;
                        }
                    }
                    break;
                case SortStatus.AlignReq:
                    subStatusCode = "10: SortST_对位请求";
                    {
                        if (SortSTFinalAlignOK)
                        {
                            SortStatusCode = SortStatus.AlignComplete;
                        }
                        else if(!SortSTFinalAlignOK && SortSTFinalAlignNGPass)
                        {
                            SortStatusCode = SortStatus.DieUnloadReqComplete;
                        }
                    }
                    break;
                case SortStatus.AlignComplete:
                    subStatusCode = "12: 对位完成";
                    {
                        if (!DryRunMode)
                        {
                            if ((LDTRStatusCode == LDTRStatus.ToLoad||LDTRStatusCode == LDTRStatus.WaitTR_1_LDAndTR_2_ULD) && !LDTR_1_Pass/* && LDTR_1_Empty*/)
                            {
                                if (SortSTFinalAlignForTR1_X <= -140 && SortSTFinalAlignForTR1_X >= -360 && SortSTFinalAlignForTR1_Y <= -200 && SortSTFinalAlignForTR1_Y >= -470 && SortSTFinalAlignForTR1_R <= 92 && SortSTFinalAlignForTR1_R >= -180 && SortSTFinalAlignForTR1_StageR <= 91 && SortSTFinalAlignForTR1_StageR >= -95)
                                {
                                    SortPos_Sort_X = SortSTFinalAlignForTR1_X;
                                    SortPos_Sort_Y = SortSTFinalAlignForTR1_Y;
                                    SortPos_Sort_R = SortSTFinalAlignForTR1_StageR;
                                    SortPos_LDTR_1_R = SortSTFinalAlignForTR1_R;

                                    SortStatusCode = SortStatus.ToDieUnloadAfterAlign;
                                }
                                else
                                {
                                    if (!(SortSTFinalAlignForTR1_X <= -140 && SortSTFinalAlignForTR1_X>= -360))
                                    {
                                        ProcedureHelper_Exc.LogMsg(string.Format("精定位-挑拣LDTR1,X位置超范围：结果[OK:-360<= X <= -140]:{0}", SortSTFinalAlignForTR1_X));
                                    }
                                    if (!(SortSTFinalAlignForTR1_Y <= -200 && SortSTFinalAlignForTR1_Y >= -470))
                                    {
                                        ProcedureHelper_Exc.LogMsg(string.Format("精定位-挑拣LDTR1,Y位置超范围：结果[OK:-470<= Y <= -200]:{0}", SortSTFinalAlignForTR1_Y));
                                    }
                                    if (!(SortSTFinalAlignForTR1_StageR <= 91 && SortSTFinalAlignForTR1_StageR >= -95))
                                    {
                                        ProcedureHelper_Exc.LogMsg(string.Format("精定位-挑拣LDTR1,StageR位置超范围：结果[OK:-95<= R <=91]:{0}", SortSTFinalAlignForTR1_StageR));
                                    }
                                    if (!(SortSTFinalAlignForTR1_R <= 92 && SortSTFinalAlignForTR1_R >= -180))
                                    {
                                        ProcedureHelper_Exc.LogMsg(string.Format("精定位-挑拣LDTR1,R位置超范围：结果[OK:-180<= R <=92]:{0}", SortSTFinalAlignForTR1_R));
                                    }
                                    WaferResult_ToMotion.Dies[CurrentDieIndex].Taked = true;
                                    SortStatusCode = SortStatus.CheckStatus;
                                }
                            }
                            else if((LDTRStatusCode == LDTRStatus.ToUnload || LDTRStatusCode == LDTRStatus.WaitTR_1_ULDAndTR_2_LD) && !LDTR_2_Pass )
                            {
                                if (SortSTFinalAlignForTR2_X <= -140 && SortSTFinalAlignForTR2_X >= -360 && SortSTFinalAlignForTR2_Y <= -200 && SortSTFinalAlignForTR2_Y >= -470 && SortSTFinalAlignForTR2_R <= 92 && SortSTFinalAlignForTR2_R >= -180 && SortSTFinalAlignForTR2_StageR <= 91 && SortSTFinalAlignForTR2_StageR >= -95)
                                {
                                    SortPos_Sort_X = SortSTFinalAlignForTR2_X;
                                    SortPos_Sort_Y = SortSTFinalAlignForTR2_Y;
                                    SortPos_Sort_R = SortSTFinalAlignForTR2_StageR;
                                    SortPos_LDTR_2_R = SortSTFinalAlignForTR2_R;

                                    SortStatusCode = SortStatus.ToDieUnloadAfterAlign;
                                }
                                else
                                {
                                    if (!(SortSTFinalAlignForTR2_X <= -140 && SortSTFinalAlignForTR2_X >= -360))
                                    {
                                        ProcedureHelper_Exc.LogMsg(string.Format("精定位-挑拣LDTR2,X位置超范围：结果[OK:-360<= X <= -140]:{0}", SortSTFinalAlignForTR2_X));
                                    }
                                    if (!(SortSTFinalAlignForTR2_Y <= -200 && SortSTFinalAlignForTR2_Y >= -470))
                                    {
                                        ProcedureHelper_Exc.LogMsg(string.Format("精定位-挑拣LDTR2,Y位置超范围：结果[OK:-470<= Y <= -200]:{0}", SortSTFinalAlignForTR1_Y));
                                    }
                                    if (!(SortSTFinalAlignForTR2_StageR <= 91 && SortSTFinalAlignForTR2_StageR >= -95))
                                    {
                                        ProcedureHelper_Exc.LogMsg(string.Format("精定位-挑拣LDTR2,StageR位置超范围：结果[OK:-95<= R <=91]:{0}", SortSTFinalAlignForTR2_StageR));
                                    }
                                    if (!(SortSTFinalAlignForTR2_R <= 92 && SortSTFinalAlignForTR2_R >= -180))
                                    {
                                        ProcedureHelper_Exc.LogMsg(string.Format("精定位-挑拣LDTR2,R位置超范围：结果[OK:-180<= R <=92]:{0}", SortSTFinalAlignForTR1_R));
                                    }
                                    WaferResult_ToMotion.Dies[CurrentDieIndex].Taked = true;
                                    SortStatusCode = SortStatus.CheckStatus;
                                }
                            }
                        }
                        else
                        {
                            SortPos_Sort_X = ParameterHelper.SysParam.SortST.Wait.X;
                            SortPos_Sort_Y = ParameterHelper.SysParam.SortST.Wait.Y;
                            SortPos_Sort_R = 0;
                            SortStatusCode = SortStatus.ToDieUnloadAfterAlign;
                        }
                    }
                    break;
                case SortStatus.ToDieUnloadAfterAlign:
                    subStatusCode = "13: SortST_XYR移动到DieULD补正位";
                    {
                        if (SortST_X_Sort && SortST_Y_Sort && SortST_R_Sort
                            && ((LDTRStatusCode == LDTRStatus.WaitTR_1_LDAndTR_2_ULD )
                         || (LDTRStatusCode == LDTRStatus.WaitTR_1_ULDAndTR_2_LD)))
                        {
                            LDTR1_GetFail = false;
                            LDTR2_GetFail = false;
                            SortStatusCode = SortStatus.DieUnloadReq;
                        }
                    }
                    break;
                case SortStatus.DieUnloadReq:
                    subStatusCode = "15: SortST DieULD请求";
                    {
                        if(LDTR2GetStatusCode== TRGetStatus.Init 
                            && LDTR1GetStatusCode == TRGetStatus.Init 
                            && LDTR1PutStatusCode== TRPutStatus.Init 
                            && LDTR2PutStatusCode == TRPutStatus.Init 
                            && ProcedureHelper_EQ.InPutStop)
                        {
                            SortStatusCode = SortStatus.Init;
                        }//20230314liangSort退Tray
                        else if ((LDTRStatusCode == LDTRStatus.WaitTR_1_LDAndTR_2_ULD && (/*LDTR_1_Presence || */LDTR1_GetFail))
                         || (LDTRStatusCode == LDTRStatus.WaitTR_1_ULDAndTR_2_LD && (/*LDTR_2_Presence ||*/ LDTR2_GetFail)))
                        {
                            SortStatusCode = SortStatus.DieUnloadReqComplete;
                        }
                    }
                    break;
                case SortStatus.DieUnloadReqComplete:
                    subStatusCode = "16: SortST DieULD完成";
                    {
                        SortSTFinalAlignPass = false;
                        SortSTFinalAlignNGPass = false;
                        SortSTFinalAlignOK = false;
                        if ((LDTR1GetStatusCode == TRGetStatus.Init && LDTR_1_Z_Wait)
                         || (LDTR2GetStatusCode == TRGetStatus.Init && LDTR_2_Z_Wait))
                        {
                            if (!DryRunMode && WaferResult_ToMotion != null)
                            {
                                WaferResult_ToMotion.Dies[CurrentDieIndex].Taked = true;
                                string ID = ParameterHelper.SortST.Wafer_ID;
                                string pathdest = Environment.CurrentDirectory + string.Format(@"\WaferResult_ToMotion\{0}", ID.ToString());
                                Utilities.SerializeHelper.Serialize<WaferResult_ToMotion>(WaferResult_ToMotion, pathdest);
                                CurrentDieIndex++;
                                SortStatusCode = SortStatus.CheckStatus;
                            }
                            else if (DryRunMode)
                            {
                                if (count > 50)
                                {
                                    count = 0;
                                    //ParameterHelper.SortST.SortCompletFlag = true;
                                    SortStatusCode = SortStatus.CheckStatus;
                                }
                                else
                                {
                                    count++;
                                    SortStatusCode = SortStatus.AlignReq;
                                }
                            }
                        }
                    }
                    break;
                default:
                    break;
            }

            LogStatusCode(subStatusCode);
        }

        private void LogStatusCode(string msg)
        {
            if (msg != tempStatusCode)
            {
                tempStatusCode = msg;
                LogMsg("挑拣- " + tempStatusCode);
            }
        }
    }
}
